package com.ruoyi.system.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.ruoyi.system.domain.AlpStageBudget;
import com.ruoyi.system.service.IAlpContractStageService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.AlpStagePredict;
import com.ruoyi.system.service.IAlpStagePredictService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 预测收款Controller
 * 
 * @author zhangjun
 * @date 2020-02-15
 */
@Controller
@RequestMapping("/system/predict")
public class AlpStagePredictController extends BaseController
{
    private String prefix = "system/predict";

    @Autowired
    private IAlpStagePredictService alpStagePredictService;

    @Autowired
    private IAlpContractStageService alpContractStageService;

    @RequiresPermissions("system:predict:view")
    @GetMapping()
    public String predict()
    {
        return prefix + "/predict";
    }

    /**
     * 查询预测收款列表
     */
    @RequiresPermissions("system:predict:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AlpStagePredict alpStagePredict)
    {
        startPage();
        List<AlpStagePredict> list = alpStagePredictService.selectAlpStagePredictList(alpStagePredict);
        return getDataTable(list);
    }

    /**
     * 导出预测收款列表
     */
    @RequiresPermissions("system:predict:export")
    @Log(title = "预测收款", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AlpStagePredict alpStagePredict)
    {
        List<AlpStagePredict> list = alpStagePredictService.selectAlpStagePredictList(alpStagePredict);
        ExcelUtil<AlpStagePredict> util = new ExcelUtil<AlpStagePredict>(AlpStagePredict.class);
        return util.exportExcel(list, "predict");
    }

    /**
     * 新增预测收款
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增预测收款
     */
    @GetMapping("/add/{stageId}")
    public String add(@PathVariable("stageId") Long stageId, ModelMap mmap)
    {
        mmap.put("alpStageDetail", alpContractStageService.getStageDetail(stageId));
        return prefix + "/addFromStage";
    }

    /**
     * 新增保存预测收款
     */
    @RequiresPermissions("system:predict:add")
    @Log(title = "预测收款", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AlpStagePredict alpStagePredict)
    {
        alpStagePredict.setUpdatedTime(new Date());
        alpStagePredict.setDelFlag(0);
        return toAjax(alpStagePredictService.insertAlpStagePredict(alpStagePredict));
    }

    /**
     * 修改预测收款
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        AlpStagePredict alpStagePredict = alpStagePredictService.selectAlpStagePredictById(id);
        mmap.put("alpStagePredict", alpStagePredict);
        return prefix + "/edit";
    }

    /**
     * 修改保存预测收款
     */
    @RequiresPermissions("system:predict:edit")
    @Log(title = "预测收款", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AlpStagePredict alpStagePredict)
    {
        return toAjax(alpStagePredictService.updateAlpStagePredict(alpStagePredict));
    }

    /**
     * 删除预测收款
     */
    @RequiresPermissions("system:predict:remove")
    @Log(title = "预测收款", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(alpStagePredictService.deleteAlpStagePredictByIds(ids));
    }

    /**
     * 可编辑表格--编辑预测金额
     */
    @GetMapping("/editpredict/{stageId}")
    public String editTablePredict(@PathVariable("stageId") Long stageId, ModelMap mmap)
    {
        mmap.put("stageId", stageId);
        return prefix + "/editablePredict";
    }


    /**
     * 查询预测收款列表
     */
    @RequiresPermissions("system:predict:list")
    @PostMapping("/listTable/{stageId}")
    @ResponseBody
    public TableDataInfo listTableActual(@PathVariable("stageId") Long stageId, ModelMap mmap)
    {
        startPage();
        List<AlpStagePredict> list = alpStagePredictService.selectAlpStagePredictByStageId(stageId);
        return getDataTable(list);
    }


    /**
     * 修改保存预测收款
     */
    @RequiresPermissions("system:predict:edit")
    @Log(title = "预测收款", businessType = BusinessType.UPDATE)
    @PostMapping("/editOneLine/{id}/{predictMoney}/{predictTime}")
    @ResponseBody
    public AjaxResult editOneLine(@PathVariable("id") Long id,
                                  @PathVariable("predictMoney") BigDecimal predictMoney,
                                  @PathVariable("predictTime") Date predictTime)
    {
        AlpStagePredict predict = alpStagePredictService.selectAlpStagePredictById(id); //必须在前
        //重新插入一条
        predict.setId(null);
        if (null != predictMoney) {
            predict.setPredictMoney(predictMoney);
        }
        if (null != predictTime) {
            predict.setPredictTime(predictTime);
        }
        predict.setUpdatedTime(new Date());
        //将原来的记录 置删除标记为1
        alpStagePredictService.deleteAlpStagePredictById(id);//必须在后

        return toAjax(alpStagePredictService.insertAlpStagePredict(predict));
    }

    /**
     * 删除预测收款
     */
    @RequiresPermissions("system:predict:remove")
    @PostMapping( "/remove/{id}")
    @ResponseBody
    public AjaxResult removeById(@PathVariable("id") Long id)
    {
        return toAjax(alpStagePredictService.deleteAlpStagePredictById(id));
    }

    /**
     * 查询预测收款列表
     */
    @RequiresPermissions("system:contractDetail:moneyBack")
    @PostMapping("/listPredictTable/{contractId}")
    @ResponseBody
    public TableDataInfo listPredictTable(@PathVariable("contractId") Long contractId, ModelMap mmap)
    {
        startPage();
        AlpStagePredict alpStagePredict = new AlpStagePredict();
        alpStagePredict.setContractId(contractId);
        List<AlpStagePredict> list = alpStagePredictService.selectAlpStagePredictList(alpStagePredict);
        return getDataTable(list);
    }
}
